<template>
  <div class="app-container">
    <el-card class="mb20">
      <el-form :model="listQF" inline>
        <el-form-item label="物料编号">
          <el-input v-model="listQF.code" placeholder="物料编号"></el-input>
        </el-form-item>
        <el-form-item label="物料名称">
          <el-input v-model="listQF.name" placeholder="物料名称"></el-input>
        </el-form-item>
        <el-form-item label="型号">
          <el-input v-model="listQF.model" placeholder="型号"></el-input>
        </el-form-item>
        <el-form-item label="单位">
          <el-input v-model="listQF.unit" placeholder="单位"></el-input>
        </el-form-item>
        <el-form-item label="单价">
          <el-input v-model="listQF.price" placeholder="单价"></el-input>
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="listQF.type" placeholder="类型">
            <el-option value="成品">成品</el-option>
            <el-option value="原料">原料</el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getList">查询</el-button>
          <el-button @click="ResetList">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="4">
          <el-tree
            :data="options"
            :props="{ label: 'name', value: 'id' }"
            :default-expand-all="true"
            @node-click="handleNodeClick"
          ></el-tree>
        </el-col>
        <el-col :span="20">
          <el-button size="mini" @click="OpenAddDialog" type="success"
            >新增</el-button
          >
          <el-button
            size="mini"
            @click="OpenEditDialogU"
            type="success"
            :disabled="ids.length != 1"
            >修改</el-button
          >
          <el-button size="mini" @click="handleDel(true)" type="danger"
            >删除</el-button
          >
          <el-table
            style="margin-top: 20px"
            :header-cell-style="{ background: '#f8f8f8' }"
            :data="list"
            @selection-change="selectionChange"
          >
            <el-table-column type="selection"></el-table-column>
            <el-table-column
              v-for="item in listColumn"
              :key="item.prop"
              :label="item.label"
              :prop="item.prop"
            ></el-table-column>
            <el-table-column label="操作" v-slot="{ row }" width="280px">
              <div>
                <el-button
                  type="primary"
                  size="mini"
                  @click="OpenEditDialog(row)"
                  >修改</el-button
                >
                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDel(false, row.id)"
                  >删除</el-button
                >
              </div>
            </el-table-column>
          </el-table>
          <el-pagination
            style="display: flex; justify-content: right"
            @size-change="PagerLimitChange"
            @current-change="PagerPageChange"
            :current-page="pager.page"
            :page-sizes="[2, 10, 30, 40]"
            :page-size="pager.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pager.total"
          >
          </el-pagination>
        </el-col>
      </el-row>
    </el-card>
    <el-dialog
      center
      width="1200px"
      :visible="visible"
      :title="title"
      @close="close"
      :close-on-click-modal="false"
    >
      <el-form inline ref="listRef" :model="listEF">
        <el-form-item label="分类" prop="category" required>
          <el-cascader
            v-model="listEF.category"
            :options="options"
            :props="{
              checkStrictly: true,
              label: 'name',
              value: 'id',
              emitPath: false,
            }"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="物料编号" prop="code">
          <el-input v-model="listEF.code" placeholder="物料编号"></el-input>
        </el-form-item>
        <el-form-item label="物料名称" prop="name" required>
          <el-input v-model="listEF.name" placeholder="物料名称"></el-input>
        </el-form-item>
        <el-form-item label="型号" prop="model">
          <el-input v-model="listEF.model" placeholder="型号"></el-input>
        </el-form-item>
        <el-form-item label="规格" prop="specification">
          <el-input
            v-model="listEF.specification"
            placeholder="规格"
          ></el-input>
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="listEF.unit" placeholder="单位"></el-input>
        </el-form-item>
        <el-form-item label="单价" prop="price">
          <el-input v-model="listEF.price" placeholder="单价"></el-input>
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="listEF.type" placeholder="类型">
            <el-option value="成品">成品</el-option>
            <el-option value="原料">原料</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            type="textarea"
            v-model="listEF.remark"
            placeholder="备注"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div>
          <el-button @click="close">取消</el-button>
          <el-button type="primary" @click="handlesubmit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getCustomerList } from '@/api_zc/task/sale-order-approve'
import { getMaterialList, delMaterial, editMaterial, addMaterial } from '@/api_zc/mes/bom'
import { getMaterialCatgoryChildrenList } from '@/api_zc/wms/material-category'
export default {
  mounted () {
    this.getList()
    this.getMaterialCatgoryChildrenList()
  },
  data () {
    return {
      pager: {
        limit: 10,
        page: 1,
        total: 0
      },
      list: [],
      listColumn: [
        {
          label: '编号',
          prop: 'id'
        },
        {
          label: '物料编码',
          prop: 'code'
        },
        {
          label: '物料名称',
          prop: 'name'
        },
        {
          label: '型号',
          prop: 'model'
        },
        {
          label: '规格',
          prop: 'specification'
        },
        {
          label: '单位',
          prop: 'unit'
        },
        {
          label: '单价',
          prop: 'price'
        },
        {
          label: '类型',
          prop: 'category.name'
        },
        {
          label: '备注',
          prop: 'remark'
        },
      ],
      listQF: {
        code: '',
        name: '',
        model: '',
        specification: '',
        unit: '',
        price: '',
        pid: '',
        type: ''
      },
      listEF: {
        id: '',
        category: '',
        code: '',
        name: '',
        model: '',
        specification: '',
        unit: '',
        price: '',
        type: '',
        remark: ''
      },
      visible: false,
      title: '修改销售退货',
      ids: [],
      options: [],
      editer: {}
    }
  },
  methods: {
    async getMaterialCatgoryChildrenList () {
      const { data } = await getMaterialCatgoryChildrenList()
      this.options = data
    },
    async getUnityList () {
      const { data: { content } } = await getCustomerList()
      this.customerList = content
    },
    async getList () {
      const { data: { total, content } } = await getMaterialList({ ...this.listQF, page: this.pager.page, limit: this.pager.limit })
      this.list = content
      this.pager.total = total
    },
    ResetList () {
      Object.assign(this.listQF, this.$options.data().listQF)
      this.getList()
    },
    PagerLimitChange (val) {
      this.pager.limit = val
      this.getList()
    },
    PagerPageChange (val) {
      this.pager.page = val
      this.getList()
    },
    OpenEditDialog (row) {
      this.isAdd = false
      this.visible = true
      this.$nextTick(() => {
        for (let key in row) {
          if (row.hasOwnProperty(key)) {
            if (this.listEF.hasOwnProperty(key)) {
              if (key == 'category') {
                this.listEF[key] = row[key].id
                return
              }
              this.listEF[key] = row[key]
            }
          }
        }
      })
      this.title = '修改物料档案管理'
    },
    OpenEditDialogU () {
      this.isAdd = false
      this.visible = true
      this.$nextTick(() => {
        for (let key in this.editer) {
          if (this.editer.hasOwnProperty(key)) {
            if (this.listEF.hasOwnProperty(key)) {
              if (key == 'category') {
                this.listEF[key] = this.editer[key].id
                return
              }
              this.listEF[key] = this.editer[key]
            }
          }
        }
      })
      this.title = '修改物料档案管理'
    },
    OpenAddDialog () {
      this.isAdd = true
      this.visible = true
      this.title = '添加物料档案管理'
      this.$nextTick(() => {
        for (let key in this.listEF) {
          this.listEF[key] = ''
        }
        this.$refs.listRef.resetFields()
      })
    },
    close () {
      this.visible = false
      this.listEF = {
        id: '',
        category: '',
        code: '',
        name: '',
        model: '',
        specification: '',
        unit: '',
        price: '',
        type: '',
        remark: ''
      }
    },
    handleDel (status, id) {
      let ids = []
      if (status) {
        ids = this.ids
      } else {
        ids[0] = id
      }
      this.$confirm(`是否确认删除销售退货编号为${ids.join(',')}的数据项？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await delMaterial({ ids })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    selectionChange (selectArray) {
      this.ids = selectArray.map(item => item.id)
      this.editer = selectArray[0]
    },
    handlesubmit () {
      this.$refs.listRef.validate(async (callback) => {
        if (!callback) {
          return
        }
        if (this.isAdd) {
          await addMaterial({ ...this.listEF, })
          this.$message.success('新建成功')
        } else {
          await editMaterial({ ...this.listEF, })
          this.$message.success('修改成功')
        }
        this.visible = false
        this.getList()
        this.$refs.listRef.resetFields()
      })
    },
    handleNodeClick (val) {
      this.listQF.pid = val.pid
      this.getList()
    }
  }
}
</script>

<style>
</style>